package com.shizhanzhe.zxbapp.vip;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.PDDSC;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.home.PDDSourceAdapter;
import com.shizhanzhe.zxbapp.my.ShareCodeActivity;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.utils.Util;
import com.shizhanzhe.zxbapp.widget.ShareWxPopupWindow;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by zz9527 on 2018/12/19.
 */
public class PDDSourceFragment extends Fragment {
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.backarea)
    RelativeLayout backarea;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    Unbinder unbinder;
    @BindView(R.id.top_back)
    ImageView topBack;

    private PDDSourceAdapter mpddAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pddsource, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SmartRefreshLayout refresh = view.findViewById(R.id.refresh);
        refresh.setRefreshHeader(new MaterialHeader(getActivity()));
        refresh.setRefreshFooter(new ClassicsFooter(getActivity()));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page=1;
                sourceList();
                refreshLayout.finishRefresh(2000);
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                sourceList();
                refreshLayout.finishLoadMore(2000);
            }
        });
        recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        mpddAdapter = new PDDSourceAdapter(getActivity());
        recycler.setAdapter(mpddAdapter);
        topTitle.setText("发朋友圈");
        view.findViewById(R.id.top_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        setListener();
        sourceList();
    }

    private void setListener() {
        mpddAdapter.setListener(new PDDSourceAdapter.PDDSourceListener() {
            @Override
            public void copyText(String text) {
                ClipboardManager cm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(text);
                Toast.makeText(getContext(), "复制成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void shareEwm() {
                startActivity(new Intent(getContext(), ShareCodeActivity.class));
            }

            @Override
            public void saveTI(PDDSC.DataBean item) {
                //保存图文
                Util.GetImageInputStream(getContext(), Arrays.asList(item.getImg().split(",")));
                ClipboardManager cm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(item.getContent());
                Toast.makeText(getContext(), "图文保存成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void extension(final PDDSC.DataBean item) {
                //一键推广
                bean = item;
                final ShareWxPopupWindow wxPopupWindow = new ShareWxPopupWindow(getActivity());
                wxPopupWindow.setWxListener(new ShareWxPopupWindow.ShareWxListener() {
                    @Override
                    public void shareFriend() {
                        wxPopupWindow.dismiss();
                        Wechat.ShareParams sp = new Wechat.ShareParams();
                        sp.setTitle(bean.getTitle());
                        sp.setText(bean.getContent());
                        sp.setUrl(bean.getTuiurl().getWe_app_web_view_short_url());
                        sp.setImageUrl(Path.BASE_URL + Arrays.asList(bean.getImg().split(",")).get(0));
                        sp.setShareType(Platform.SHARE_WEBPAGE);
                        Platform weChat = ShareSDK.getPlatform(Wechat.NAME);
                        weChat.share(sp);
                    }

                    @Override
                    public void shareCircle() {
                        wxPopupWindow.dismiss();
                        ClipboardManager cm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                        cm.setText(item.getContent());
                        Toast.makeText(getContext(), "推广文本已复制", Toast.LENGTH_SHORT).show();
                        WechatMoments.ShareParams sp = new WechatMoments.ShareParams();
                        sp.setTitle(bean.getTitle());
                        sp.setUrl(bean.getTuiurl().getWe_app_web_view_short_url());
//                        sp.setImageUrl(Path.BASE_URL+Arrays.asList(bean.getImg().substring(1, bean.getImg().length()).split(",")).get(0));
                        sp.setShareType(Platform.SHARE_WEBPAGE);
                        Platform Moments = ShareSDK.getPlatform(WechatMoments.NAME);
                        Moments.share(sp);
                    }

                    @Override
                    public void shareSave() {
                        wxPopupWindow.dismiss();
                        //保存图片
                        Util.GetImageInputStream(getContext(), Arrays.asList(bean.getImg().substring(1, bean.getImg().length()).split(",")));
                        ClipboardManager cm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                        cm.setText(item.getContent() + "\n" + item.getTuiurl().getWe_app_web_view_short_url());
                        Toast.makeText(getContext(), "图文保存成功", Toast.LENGTH_SHORT).show();
                    }
                });
                wxPopupWindow.showDown(topBack);
            }
        });
    }

    PDDSC.DataBean bean;
    int page=1;
    private void sourceList() {

        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.pddmaterial + UserBean.uid+"&page="+page, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        Gson gson = new Gson();
                        PDDSC tiBean = gson.fromJson(json, PDDSC.class);
                        if (tiBean.getData().size() > 0) {
                            mpddAdapter.updateData(tiBean.getData(),page);
                        } else {
                            Toast.makeText(getContext(), "暂无商品素材", Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                    }
                } else {
                    Toast.makeText(getContext(), "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


}
